import java.util.Scanner;

public class HashtabDemo {
    public static void main(String[] args) {
        HashTab hashTab = new HashTab(7);
        String key = "";
        Scanner scanner = new Scanner(System.in);

        while(true){
            System.out.println("add:添加用户");
            System.out.println("list:打印用户");

            key = scanner.next();
            switch (key){
                case "add" :
                    System.out.println("输入id");
                    int id = scanner.nextInt();
                    System.out.println("输入name");
                    String name = scanner.next();
                    Emp emp = new Emp(id,name);
                    hashTab.add(emp);
                    break;
                case "list":
                    hashTab.list();
            }
        }
    }

}

//创建雇员类
class Emp{
    public int id;
    public String name;
    public Emp next;

    public Emp(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
}

//创建雇员链表类
class EmpLinkedList{
    //设置头指针
    private Emp head;

    //向链表中加入雇员信息
    public void add(Emp emp){
        //当链表为空时，直接在表头加入
        if(head == null){
            head = emp;
            return;
        }
        //不为空时，创建新的当前雇员对象
        Emp curEmp = head;
        while(true){
            if(curEmp.next == null){
                break;
            }
            curEmp = curEmp.next;
        }
        curEmp.next = emp;
    }

    //打印链表雇员信息
    public void list(int no){
        if(head == null){
            System.out.println("链表为空");
            return;
        }
        Emp curEmp = head;
        while(true){
            System.out.printf(" => id=%d name=%s\t", curEmp.id, curEmp.name);
            if(curEmp.next == null){
                break;
            }
            curEmp = curEmp.next;
        }
        System.out.println();
    }
}

//创建HashTab管理雇员链表
class HashTab{
    private EmpLinkedList[] empLinkedListsArray;
    private int size;

    public HashTab(int size) {
        this.size = size;
        empLinkedListsArray = new EmpLinkedList[size];
        //对哈希表中的所有雇员链表初始化
        for(int i = 0; i < size; i++) {
            empLinkedListsArray[i] = new EmpLinkedList();
        }
    }

    //添加雇员信息
    public void add(Emp emp){
        int empLinkedListNo = HashFun(emp.id);
        empLinkedListsArray[empLinkedListNo].add(emp);
    }

    //打印所有的雇员信息
    public void list(){
        for(int i = 0; i < size; i++){
            empLinkedListsArray[i].list(i);
        }
    }

    public int  HashFun(int id){
        return id % size;
    }
}